package org.example.sword2offer.primary;

import java.util.Stack;

/**
 * @author: lynn
 * @Descript: 翻转单词顺序列
 * student. a am I”。后来才意识到，这家伙原来把句子单词的顺序翻转了，正确的句子应该是“I am a student.”
 * @date: 2021/3/21 22:36
 * @version: 1.0
 */
public class Q44_LeftRotateString {
    public static void main(String[] args) {
        //String  str="I am a student.";
        String  str="   "; //坑的地方，就是全是"    "，这种判断比较容易采坑
        System.out.println(reverse(str));
    }
    private static String reverse(String str){
        StringBuilder reverseStr=new StringBuilder();

        String[] strs=str.split(" ");
        if (strs.length==0){
            return str;
        }
        Stack<String> stack=new Stack<>();
        for(String s:strs){
            stack.push(s);
        }
        while (!stack.isEmpty()){

            reverseStr.append(stack.pop());
            if (!stack.isEmpty()) reverseStr.append(" ");
        }
        return reverseStr.toString();
    }
}


